package br.com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import br.com.bean.CadHistoricoFinalBean;

public class CadHistoricoFinalDao {

	public void inserir (Connection conn, CadHistoricoFinalBean value) throws Exception{
		
		String sql = "INSERT INTO cad_historico_final (id_aluno, id_cad_historico_letivo, id_cad_historico_nota_falta" +
				"carga_horaria_final, resultado_final, carga_horaria_total) VALUES (?, ?, ?, ?, ?, ?)";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1,value.getIdaluno().getId());
		ps.setInt(2, value.getIdcadhistletivo().getId());
		ps.setInt(3, value.getIdcadhistnotafalta().getId());
		ps.setInt(4, value.getCargahrfinal());
		ps.setString(5, value.getResfinal());
		ps.setInt(6, value.getCargahrtotal());
		ps.execute();
		ps.close();
		
	}
	
	public void alterar (Connection conn, CadHistoricoFinalBean value) throws Exception{
		
		String sql = "UPDATE cad_historico_final SET id_aluno = ?, id_cad_historico_letivo = ?, id_cad_historico_nota_falta = ?" +
				"carga_horaria_final = ?, resultado_final = ?, carga_horaria_total = ? WHERE  id = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1,value.getIdaluno().getId());
		ps.setInt(2, value.getIdcadhistletivo().getId());
		ps.setInt(3, value.getIdcadhistnotafalta().getId());
		ps.setInt(4, value.getCargahrfinal());
		ps.setString(5, value.getResfinal());
		ps.setInt(6, value.getCargahrtotal());
		ps.execute();
		ps.close();

	}
	
	public void excluir (Connection conn, CadHistoricoFinalBean value) throws Exception {

		String sql = "DELETE FROM cad_historico_final WHERE where id = ?";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getId());
		ps.executeUpdate();
	}
	
	public ArrayList<CadHistoricoFinalBean> getLista(Connection conn, CadHistoricoFinalBean value) throws Exception{

		String sql = "SELECT id_aluno, id_cad_historico_letivo, id_cad_historico_nota_falta" +
				"carga_horaria_final, resultado_final, carga_horaria_total FROM cad_historico_final where id = ?";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getId());
		ResultSet rs = ps.executeQuery();
		ArrayList<CadHistoricoFinalBean> cadhistoricofinal = new ArrayList<CadHistoricoFinalBean>();
		while(rs.next()){
			CadHistoricoFinalBean a = new CadHistoricoFinalBean();
			a.getIdaluno().setId(rs.getInt("id_aluno"));
			a.getIdcadhistletivo().setId(rs.getInt("id_cad_historico_letivo"));
			a.getIdcadhistnotafalta().setId(rs.getInt("id_cad_historico_nota_falta"));
			a.setCargahrfinal(rs.getInt("carga_horaria_final"));
			a.setResfinal(rs.getString("resultado_final"));
			a.setCargahrtotal(rs.getInt("carga_horaria_total"));
			cadhistoricofinal.add(a);
		}
		return cadhistoricofinal;
	}
}
